import { reqIndexData, reqCategoryData } from '@/api/home';

const state = {
  indexData: {},
  categoryData: {},
};
const mutations = {
  SET_INDEXDATA(state, indexData) {
    state.indexData = indexData;
  },
  SET_CATEGORYDATA(state, categoryData) {
    state.categoryData = categoryData;
  },
};
const actions = {
  async getIndexData({ commit }) {
    const result = await reqIndexData();
    if (result.code === '200') {
      commit('SET_INDEXDATA', result);
    } else {
      console.log(result.message);
    }
  },
  async getCategoryData({ commit }, currentId) {
    const result = await reqCategoryData(currentId);
    console.log(result.data);
    commit('SET_CATEGORYDATA', result.data);
  },
};
const getters = {
  indexData() {
    return (state.indexData.data || {}).data || {};
  },
  categoryItemList() {
    return state.categoryData.categoryItemList || [];
  },
  currentCategory() {
    return state.categoryData.currentCategory || {};
  },
};

export default {
  state,
  mutations,
  actions,
  getters,
};
